package c.francis.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import c.francis.pojo.Manager;
import c.francis.service.impl.ManagerServiceImpl;
@Controller
public class ManagerController {
	
	@Autowired
	private ManagerServiceImpl managerServiceImpl;
	
	/**
	 * @author TsaiHsibei
	 * @note �ṩһ��Ĭ�ϵĵ�¼���
	 */
	@RequestMapping("goadminLogin")
	public String goadminLogin() {
		return "admin_login";
	}
	
	/**
	 * @author TsaiHsibei
	 * @note ������Ա��¼
	 */
	@RequestMapping("checkadminLogin")
	public @ResponseBody int checklogin(String username ,String password,HttpSession session) {
		try {
			Manager manager = managerServiceImpl.getManager(username, password);
			if(manager!=null) {
				session.setAttribute("manager", manager);
				return 1;
			}else {
				return 2;
			}
		} catch (Exception e) {
			return 0;
		}
	}
	
	/**
	 * @author TsaiHsibei
	 * @note ����¼�ɹ�֮��������Ҫ��ת��һ��������ҳ�棬�����ڽ�ȥ��ʱ��Ϊ�˱���Ƿ����룬������Ҫ����session���
	 */
	@RequestMapping("goadminindex")
	public String goadminHome(HttpSession session) {
		Manager manager = (Manager) session.getAttribute("manager");
		if(manager==null) {
			return "admin_login";
		}else {
			return "admin_index";
		}
	}
	
	/**
	 * @author TsaiHsibei
	 * @note ��ת����Ϣ�޸Ľ���
	 */
	@RequestMapping("goupdatemanager")
	public String updatemanagerpage(HttpServletRequest request) {
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("manager");
		request.setAttribute("manager", manager);
		return "updatemanager";
	}

	/**
	 * 在原有基础上进行升级：
	 * 		原来->只能修改管理员相关信息
	 * 		升级后->管理员可以通过会员的相关许可去修改会员的信息
	 * @param oldpassword
	 * @param newpassword
	 * @param username
	 * @param session
	 * @return
	 */
	@RequestMapping("confirmupdatemanager")
	public @ResponseBody int confirmupdatemanager(String oldpassword,String newpassword,String username,HttpSession session) {

		//通过用户名去用户表查询用户

		//如果查询结果不为空，就更新用户密码

		//用户查询为空，就去管理员表查询管理员

		//修改管理员密码



		Manager manager = (Manager) session.getAttribute("manager");
		try {
			//��ȡ������
			if(!oldpassword.equals(manager.getPassword())) {
				return 4;
			}else {
				managerServiceImpl.updateManager(manager.getId(), username, newpassword);
				Manager manager2 = managerServiceImpl.getManager(username, newpassword);
				session.setAttribute("manager", manager2);
				return 1;
			}
		}catch (Exception e) {
			return 2;
		}
	}
	
}
